$(() => {
    init();
});

function init() {
    // isRememberMe();
    $(".login-button-box button").click(submit);
}

/* 请求Remember状态 */
function isRememberMe() {
    $.ajax({
        url: "/api/back/admin/rememberMe", type: "GET", xhrFields: {
            withCredentials: true
        }, success: (ret) => {
            if (ret.code === 0) {
                let data = ret.data;
                loginForm.username.value = data.code;
                loginForm.password.value = data.password;
            }
        }
    });
}

function submit() {
    let username = loginForm.username.value;
    let password = loginForm.password.value;
    let user = {"username": username, "password": password};
    // let verifyCode = loginForm.verifyCode.value;
    // let isRememberMe = $("input[name='rememberMe']")[0].checked;
    // if (username === "" || password === "") {
    //     alert("请输入正确的账号及密码!");
    //     return;
    // } else if (verifyCode === "") {
    //     alert("请输入验证码!");
    //     return;
    // }
    $.ajax({
        url: "/admin/login", type: "POST",
        contentType: "application/json",
        data: JSON.stringify(user), success: (ret) => {
            if (ret.code === 0) {
                console.log(ret);
                let data = ret.data;
                let authToken = data.tokenHead.concat(" ").concat(data.access_token);
                localStorage.setItem("Authorization", authToken);
                // window.location.href = "/";
                ajax("/index", "GET", "","text/html");
            } else {
                alert(ret.msg);
                loginForm.reset();
            }
        }
    });
}